package com.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.job.common.Constant;
import com.job.common.ResponseResult;
import com.job.common.converter.StudentFieldConverter;
import com.job.controller.req.MateVO;
import com.job.mapper.PositionInfoMapper;
import com.job.mapper.PositionMapper;
import com.job.mapper.StudentMapper;
import com.job.mapper.es.StudentInfoRepository;
import com.job.pojo.Position;
import com.job.pojo.PositionInfo;
import com.job.pojo.Student;
import com.job.pojo.StudentInfoPO;
import com.job.pojo.vo.StudentVO;
import com.job.service.AiMateService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class AiMateServiceImpl implements AiMateService , Constant {

    @Resource
    private StudentInfoRepository studentInfoRepository;

    @Resource
    private PositionInfoMapper positionInfoMapper;

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private StudentFieldConverter studentFieldConverter;
    @Override
    public ResponseResult<List<StudentVO>> aiMate(MateVO mateVO) {
        if (Objects.isNull(mateVO)){
            return new ResponseResult<List<StudentVO>>().setCode(400).setMessage("参数不正确");
        }

        Position position = positionMapper.selectById(mateVO.getPositionId());
        //es搜索匹配的学生
        PositionInfo positionInfo = positionInfoMapper.selectOne(new LambdaQueryWrapper<PositionInfo>()
                .eq(PositionInfo::getPositionId, position.getPositionId()));
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.multiMatchQuery(positionInfo.getContext()
                ,XL,MAJOR,PRODUCTINFO,WORKINFO,CERTIFICATE))
                .withPageable(PageRequest.of(STRAT,SIZE))
                .build();
        Page<StudentInfoPO> queryResult = studentInfoRepository.search(searchQuery);

        if (queryResult.getTotalPages() == EMPTY){
            new ResponseResult<List<Student>>().setCode(200).setMessage("抱歉，没有与该岗位匹配的学生");
        }
        List<Integer> studentIds = queryResult.get().map(studentInfoPO -> studentInfoPO.getStudentId())
                .collect(Collectors.toList());
        //保证和es返回的id顺序一致不难使用selectBatchIds
        ArrayList<StudentVO> students = new ArrayList<>();
        studentIds.forEach(studentId ->{
            Student student = studentMapper.selectById(studentId);
            students.add(studentFieldConverter.studentPoToVo(student));
        });
        return new ResponseResult<List<StudentVO>>().success(students);
    }
}

